package com.company.project.controller;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.company.project.common.utils.DataResult;
import com.company.project.common.utils.DownLoadUtil;
import com.company.project.common.utils.KeyValueUtils;
import com.company.project.common.utils.WordUtils;
import com.company.project.entity.MidCheckEntity;
import com.company.project.service.MidCheckService;
import com.company.project.vo.KeyValueVo;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.util.Date;
import java.util.List;



/**
 * 中期检查
 *
 * @author HKX
 * @email *****@mail.com
 * @date 2022-04-25 17:41:54
 */
@Controller
@RequestMapping("/")
@ConfigurationProperties(prefix = "fileload")
public class MidCheckController {

    @Autowired
    private MidCheckService midCheckService;
//    @Value("${fileload.template}")
    private String template;
//    @Value("${fileload.fileupload}")
    private String fileupload;


    public void setTemplate(String template) {
        this.template = template;
    }

    public void setFileupload(String fileupload) {
        this.fileupload = fileupload;
    }

    /**
    * 跳转到页面
    */
    @GetMapping("/index/midCheck")
    public String midCheck() {
        return "midcheck/list";
        }

    @ApiOperation(value = "新增")
    @PostMapping("midCheck/add")
    @RequiresPermissions("midCheck:add")
    @ResponseBody
    public DataResult add(@RequestBody List<KeyValueVo> list){
        MidCheckEntity midCheckEntity = new MidCheckEntity();
        midCheckEntity.setId(IdUtil.simpleUUID());
        KeyValueVo vo = list.get(0);
        vo.setValue(midCheckEntity.getId());
        list.set(0,vo);
        midCheckEntity.setStuName(KeyValueUtils.findKey(list,"stuName").getValue());
        midCheckEntity.setStuNumber(KeyValueUtils.findKey(list,"stuNumber").getValue());
        String s = JSON.toJSONString(list);
        midCheckEntity.setContentJson(s);

        KeyValueVo p12 = KeyValueUtils.findKey(list, "p12");
        p12.setValue(p12.getValue().substring(0,10));
        String format = DateUtil.format(new Date(), "yyyyMMddHHmmss");
        String wordPath=midCheckEntity.getStuName()+format+"中期检查.docx";
        midCheckEntity.setWordPath(wordPath);
        WordUtils.createWord(template+"中期检查_模板.docx",fileupload+wordPath,list);
        midCheckEntity.setCreateTime(new Date());
        midCheckEntity.setUpdateTime(new Date());
        midCheckService.save(midCheckEntity);
        return DataResult.success();
    }

    @ApiOperation(value = "删除")
    @DeleteMapping("midCheck/delete")
    @RequiresPermissions("midCheck:delete")
    @ResponseBody
    public DataResult delete(@RequestBody @ApiParam(value = "id集合") List<String> ids){
        midCheckService.removeByIds(ids);
        return DataResult.success();
    }

    @ApiOperation(value = "更新")
    @PutMapping("midCheck/update")
    @RequiresPermissions("midCheck:update")
    @ResponseBody
    public DataResult update(@RequestBody List<KeyValueVo> list){
        MidCheckEntity midCheckEntity = new MidCheckEntity();
        midCheckEntity.setId(KeyValueUtils.findKey(list,"id").getValue());
        midCheckEntity.setStuName(KeyValueUtils.findKey(list,"stuName").getValue());
        midCheckEntity.setStuNumber(KeyValueUtils.findKey(list,"stuNumber").getValue());
        String s = JSON.toJSONString(list);
        midCheckEntity.setContentJson(s);
        midCheckEntity.setUpdateTime(new Date());
        midCheckService.updateById(midCheckEntity);
        MidCheckEntity checkEntity = midCheckService.getById(midCheckEntity.getId());
        String wordPath = checkEntity.getWordPath();
        File file = new File(fileupload, wordPath);
        if(file.exists()){
            file.delete();
        }
        WordUtils.createWord(template+"/"+"中期检查_模板.docx",fileupload+wordPath,list);
        return DataResult.success();
    }

    @ApiOperation(value = "查询分页数据")
    @PostMapping("midCheck/listByPage")
    @RequiresPermissions("midCheck:list")
    @ResponseBody
    public DataResult findListByPage(@RequestBody MidCheckEntity midCheck){
        System.out.println(midCheck.getStuName());
        Page page = new Page(midCheck.getPage(), midCheck.getLimit());
        LambdaQueryWrapper<MidCheckEntity> queryWrapper = Wrappers.lambdaQuery();
        //查询条件示例
        //queryWrapper.eq(MidCheckEntity::getId, midCheck.getId());
        if (StrUtil.isNotBlank(midCheck.getStuName())){
            queryWrapper.like(MidCheckEntity::getStuName,midCheck.getStuName().trim()).or().like(MidCheckEntity::getStuNumber,midCheck.getStuName().trim());
        }
        queryWrapper.orderByDesc(MidCheckEntity::getCreateTime);
        IPage<MidCheckEntity> iPage = midCheckService.page(page, queryWrapper);
        return DataResult.success(iPage);
    }

    @ApiOperation(value = "下载")
    @PostMapping("download/midCheck/{id}")
    public void download(@PathVariable("id")String  id, HttpServletRequest request, HttpServletResponse response){
        MidCheckEntity byId = midCheckService.getById(id);
        String wordPath = byId.getWordPath();
        File file = new File(fileupload, wordPath);
        DownLoadUtil.outputDownload(file,byId.getStuName()+"中期检查表.docx",request,response);
    }

}
